package cn.aifeix.order.controller;

import cn.aifeix.basic.util.LoginContext;
import cn.aifeix.order.service.IOrderProductService;
import cn.aifeix.order.domain.OrderProduct;
import cn.aifeix.order.query.OrderProductQuery;
import cn.aifeix.basic.util.AjaxResult;
import cn.aifeix.basic.util.PageList;
import cn.aifeix.org.domain.Employee;
import cn.aifeix.org.service.EmployeeService;
import cn.aifeix.pet.dto.AcceptOrderFormDto;
import cn.aifeix.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/orderProduct")
public class OrderProductController {
    @Autowired
    public IOrderProductService orderProductService;

    @Autowired
    private EmployeeService employeeService;

    /**
     * 保存和修改公用的
     * @param orderProduct  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody OrderProduct orderProduct){
        try {
            if( orderProduct.getId()!=null)
                orderProductService.update(orderProduct);
            else
                orderProductService.add(orderProduct);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            orderProductService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public OrderProduct get(@PathVariable("id")Long id)
    {
        return orderProductService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<OrderProduct> list(){

        return orderProductService.findAll();
    }

    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<OrderProduct> json(@RequestBody OrderProductQuery query)
    {
        return orderProductService.queryByPage(query);
    }

    @PostMapping("/toHandle")
    public PageList<OrderProduct> toHandle(@RequestBody OrderProductQuery query)
    {
        query.setState(0);
        return orderProductService.toHandle(query);
    }

    @PostMapping("/finish")
    public PageList<OrderProduct> finish(@RequestBody OrderProductQuery query)
    {
//        query.setState(1);
        return orderProductService.toHandle(query);
    }

    @PostMapping("/reject/{id}")
    public AjaxResult reject(@PathVariable("id") Long id, HttpServletRequest request)
    {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeService.findByLogininfoId(logininfo.getId());
        orderProductService.reject(id, employee);
        return AjaxResult.me().success();
    }

    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody AcceptOrderFormDto dto)
    {

        orderProductService.accept(dto);
        return AjaxResult.me().success();
    }

    @GetMapping("/confirm/{id}")
    public AjaxResult confirm(@PathVariable("id") Long id)
    {
        orderProductService.confirm(id);
        return AjaxResult.me().success();
    }

    @GetMapping("/cancel/{id}")
    public AjaxResult cancel(@PathVariable("id") Long id)
    {
        orderProductService.cancel(id);
        return AjaxResult.me().success();
    }
}
